<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic;

use OSS\OssClient;
use OSS\Core\OssException;
use think\Db;

class Noticle
{
    //消息类型
    CONST REGISTER_NOTICE = 1; //注册
    CONST REAL_VERIFY_NOTICE = 2; //真人认证
    CONST VIDEO_VERIFY_NOTICE = 3; //女神认证
    CONST VIP_NOTICE = 4; //升级vip


    /**
     * User: Simony
     * Function: 发送文本系统消息
     * @param $target 目标群体
     * @param $content 内容
     * @param string $target_type 目标类型
     * @param int $msg_type 0聊天消息,1评价通知 2节目通知 3报名通知 4申请通知 5评论通知 6收益提醒通知 7消息通知 8申请消息
     * @return array
     */
    public function sendTxtSysNotice($target, $content, $msg_type = 1, $target_type = 'users', $ext = null)
    {
        $ext['msg_type'] = $msg_type;
        $ext['type'] = $msg_type;

        $platform = sysConfig('platform');
        $customer_service_member_id = $platform['customer_service_member_id'] ?? 1;
        empty($customer_service_member_id) && $customer_service_member_id = 1;

        $res = (new \easemob\EasemobIm())->sendText($customer_service_member_id, $target_type, $target, $content, $ext);
        return ds_arr($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:发送自动文本消息
     * @param $target 目标
     * @param $type 类型   注册、真人认证、女神认证
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function sendAutoTxt($target, $type)
    {
        $member = model('common/member/Member')->getInfoByID($target);
        if (empty($member)) return;

        $add_list = [];
        switch ($type) {
            case self::REGISTER_NOTICE: //注册
                $add_list[] = $this->getAddDataMessage($member, moduleConfig('reigster_notice_1'));
                $add_list[] = $this->getAddDataMessage($member, moduleConfig('reigster_notice_2'));
                break;
            case self::REAL_VERIFY_NOTICE: //真人认证
                $add_list[] = $this->getAddDataMessage($member, moduleConfig('real_verify_notice'));
                break;
            case self::VIDEO_VERIFY_NOTICE: //女神认证
                $add_list[] = $this->getAddDataMessage($member, moduleConfig('video_verify_notice'));
                break;
            case self::VIP_NOTICE: //vip
                $add_list[] = $this->getAddDataMessage($member, moduleConfig('vip_notice'));
                break;
            default:
                break;
        }

        $messageModel = model('common/message/Message');
        foreach ($add_list as $v) {
            if (empty($v)) continue;
            $res = $messageModel->add($v);
            if (!$res) continue;
            $this->sendTxtSysNotice($v['receive_member_id'], $v['content'], 7);
        }

    }

    /**
     * User: Simony
     * Function:
     * @param $member
     * @param $config_data
     * @param $current_time
     * @return array
     */
    public function getAddDataMessage($member, $config_data)
    {
        return isset($config_data[$member['sex']]) ? [
            'member_id' => -1,
            'receive_member_id' => $member['id'],
            'type' => 0,
            'relation_member_id' => 0,
            'create_time' => time(),
            'content' => $config_data[$member['sex']],
        ] : [];
    }

    /**
     * User: Simony
     * Function: 发送客服图片
     * @param $file
     * @param $filename
     * @param $target
     * @param array $ext
     * @param string $target_type
     * @return array
     */
    public function sendCustomerServiceImg($file, $filename, $target, $ext = [], $target_type = 'users')
    {
        $platform = sysConfig('platform');
        $customer_service_member_id = $platform['customer_service_member_id'] ?? 1;
        empty($customer_service_member_id) && $customer_service_member_id = 1;
        $res = (new \easemob\EasemobIm())->sendImage($file, $filename, $target, $customer_service_member_id, $target_type, $ext);
        return ds_arr($res['code'], $res['msg'], $res['result']);
    }



}